package com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.personrule;

import com.google.common.base.Strings;
import com.kingyun.gpsinspection.purificationservice.common.activiti.ActivitiConstants;
import com.kingyun.gpsinspection.purificationservice.common.activiti.ApplicationContextUtils;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.AccountRoleMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.activiti.BpmConfNode;
import com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.exception.AssignException;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 角色人员分配规则
 * @author zjh
 * @since 20170615
 */
public class RoleRule implements Rule {

    private static RoleRule roleRule;

    private RoleRule(){}

    public static RoleRule getInstance(){
        if(roleRule==null){
            roleRule=new RoleRule();
        }
        return roleRule;
    }


    @Override
    public  void assigneRule(ActivityExecution execution,TaskEntity task, BpmConfNode bpmConfUser) {
        if(Strings.isNullOrEmpty(bpmConfUser.getUserValue())){
            throw new AssignException("没有相关角色");
        }
        String[] split = bpmConfUser.getUserValue().toString().split(ActivitiConstants.PERON_SEPARATE);
        AccountRoleMapper accountRoleMapper =(AccountRoleMapper) ApplicationContextUtils.getBean(AccountRoleMapper.class);
        List<String> userIds=accountRoleMapper.getEmployeeIdsByRoleIds(Arrays.asList(split));
        if(CollectionUtils.isEmpty(userIds)){
            throw new AssignException("相关角色没有用户");
        }
        task.addCandidateUsers(userIds);
    }
}
